package cn.baizhi.controller;


import cn.baizhi.entity.Tag;
import cn.baizhi.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LZM
 * @since 2021-12-03
 */
@RestController
@RequestMapping("/tags")
public class TagController {

    @Autowired
    private TagService tagService;

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteById(@PathVariable Long id){

        tagService.removeById(id);

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping
    public ResponseEntity<Tag> add(@RequestBody Tag tag){

        tagService.save(tag);

        return new ResponseEntity<>(tag,HttpStatus.OK);
    }

    @GetMapping
    public ResponseEntity<List<Tag>> selectAll(){

        List<Tag> list = tagService.list(null);

        return new ResponseEntity<>(list,HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Tag> selectOne(@PathVariable("id") Long id){


        Tag tag = tagService.getById(id);

        return new ResponseEntity<>(tag,HttpStatus.OK);
    }



}

